package com.vote.dao;

import net.paoding.rose.jade.annotation.DAO;
import net.paoding.rose.jade.annotation.SQL;
import net.paoding.rose.jade.annotation.SQLParam;

import com.vote.domain.VoteUser;

@DAO(catalog = "vote")
public interface VoteUserDAO {
	
	public static String TABLE="vote_user";
	
	public static String INSERT_FIELD="user_name,user_password,user_email";
	
	public static String SELECT_FIELD=INSERT_FIELD+",user_id";
	
	/**
	 * 根据userId获得该user的对象
	 * @param id
	 * @return
	 */
	@SQL("select "+SELECT_FIELD+" from vote_user where user_id=:id")
	public VoteUser getUser(@SQLParam("id") int id);
	
	/**
	 * 插入user信息
	 * @param user
	 */
	@SQL("insert into vote_user("+INSERT_FIELD+") values(:model.userName,:model.userPassword,:model.userEmail)")
	public void insertUser(@SQLParam("model") VoteUser user);
	
	/**
	 * 根据user_id删除user对信息
	 * @param id
	 */
	@SQL("delete from "+TABLE+"where user_id=:id")
	public void deleteUserById(@SQLParam("id") int id);
	
	/**
	 * 更新user密码
	 * @param user
	 */
	@SQL("update "+TABLE+"set user_password=:model.userPassword where user_id=:model.userId")
	public void updateUserPassword(@SQLParam("model") VoteUser user);
	
	/**
	 * 更新user邮箱
	 * @param user
	 */
	@SQL("update "+TABLE+"set user_email=:model.userEmail where user_id=:model.userId")
	public void updateUserEmain(@SQLParam("model") VoteUser user);
	
   /**
    * 更新用户信息
    * @param user
    */
	@SQL("update "+TABLE+"set user_email=:model.userEmail and user_password=:model.userPassword where user_id=:model.userId")
	public void  updateUser(@SQLParam("model") VoteUser user);
	
	/**
	 * 根据用户名得到用户名，用于判断用户名是否重复
	 * @param userName
	 */
	@SQL("select user_name from"+TABLE+"where user_name=:userName")
	public String getUserNameByUserName(@SQLParam("userName") String userName);
	
	/**
	 * 根据用户邮箱获得用户邮箱，用于判断用户邮箱是否重复
	 * @param userEmail
	 * @return
	 */
	@SQL("select user_email from"+TABLE+"where user_email=:userEmail")
	public String getUserNameByUserEmail(@SQLParam("userEmail") String userEmail);
	
	/**
	 * 根据用户名字和密码获得用户信息，用于判断该用户是否存在
	 * @param user
	 * @return
	 */	
	@SQL("select "+SELECT_FIELD+" from "+TABLE+"where user_name=:model.userId and user_password=:model.userPassword")
	public VoteUser login(@SQLParam("model") VoteUser user);
	
	/**
	 * 获取刚插入user的ID
	 * @return
	 */
	@SQL("select LAST_INSERT_ID()")
	public int getLastInsertId();

	
}
